package com.sip.stream.helper;

import android.content.Context;
import android.util.Log;

import com.sip.stream.ClientApp;
import com.sip.stream.R;
import com.sip.stream.utils.zfy.ZFYPlatform;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

public class ZFYPlatformSettingHelper {
    private static final String TAG = "PlatformSettingHelper";

    private WeakReference<Context> contextWeakReference;

    public ZFYPlatformSettingHelper() {
        this.contextWeakReference = new WeakReference<>(ClientApp.clientApp);
    }

    public ZFYPlatformSettingHelper(Context context) {
        this.contextWeakReference = new WeakReference<>(context);
    }

    public Context getContext() {
        return this.contextWeakReference.get();
    }

    private String getString(int resId) {
        return getContext().getString(resId);
    }


    /***
     * 获取所有平台名称
     * @return
     */
    public String[] getAllSupportPlatformLocalizedName() {
        ArrayList arrayList = new ArrayList();
        if (isSupportPlatform(ZFYPlatform.GB28181)) {
            arrayList.add(getPlatformLocalizedName(ZFYPlatform.GB28181));
        }
        return (String[]) arrayList.toArray(new String[arrayList.size()]);
    }


    /***
     * 获取平台绝对路径
     * @param action 平台关键字
     * @return
     */
    public static String getPlatformHandlerClassPath(String action) {
        if (action.equals(ZFYPlatform.GB28181)){
            return "com.sip.stream.utils.zfy.handler.GSCPlatformHandler";
        }else{
            return null;
        }
    }

    public static boolean isSupportPlatform(String action) {
        String platformHandlerClassPath = getPlatformHandlerClassPath(action);
        if (platformHandlerClassPath == null) {
            Log.w(TAG, "isSupportPlatform " + action + " className == null");
            return false;
        }
        try {
            Class.forName(platformHandlerClassPath);
            return true;
        } catch (ClassNotFoundException e) {
            Log.e(TAG, e.getLocalizedMessage());
            return false;
        }
    }

    public String getPlatformLocalizedName(String action) {
        if (action.equals(ZFYPlatform.GB28181)) {
            return getString(R.string.platform_name_gb28181);
        }else{
            return null;
        }
    }

    public String getPlatformFormLocalizedName(String action) {
        return ZFYPlatform.GB28181;
    }
}
